<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>观察者模式</title>
</head>

<body>

</body>
<script>
  // 发布者
  class Dep {
    constructor() {
      this.subs = []
    }
    // 添加订阅者
    addDep(sub) {
      if (sub && sub.update) {
        this.subs.push(sub)
      }
    }
    // 发布通知
    notify() {
      this.subs.forEach(sub => {
        sub.update()
      })
    }
  }
  // 观察者
  class Watcher {
    update() {
      console.log('update')
    }
  }
  // 测试
  let dep = new Dep()
  let watcher = new Watcher()
  dep.addDep(watcher)
  dep.notify()


  // 注意

</script>

</html>